package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName SpinnerDemo
 * @Description
 * @Author wangss
 * @date 2020.05.26 15:01
 * @Version 1.0
 */
public class Spinner1Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Spinner<Student> spinner = new Spinner<>();

        ObservableList<Student> list = FXCollections.observableArrayList();

        Student s1 = new Student("s1", 11, 11);
        Student s2 = new Student("s2", 12, 12);
        Student s3 = new Student("s3", 13, 13);
        Student s4 = new Student("s4", 14, 14);
        list.addAll(s1, s2, s3, s4);

        MySpinnerValueFactory mySpinnerValueFactory = new MySpinnerValueFactory(list);
        mySpinnerValueFactory.setConverter(new StringConverter<Student>() {
            @Override
            public String toString(Student object) {
                return object.toString();
            }

            @Override
            public Student fromString(String string) {
                return null;
            }
        });
        spinner.setValueFactory(mySpinnerValueFactory);

        AnchorPane.setTopAnchor(spinner, 20d);
        AnchorPane.setLeftAnchor(spinner, 20d);
        anchorPane.getChildren().add(spinner);

        primaryStage.setScene(scene);
        primaryStage.setTitle("spinner");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

    }
}
